﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
using System.IO;

namespace WebBHXH.Entities
{
    class HuyenModel
    {
        public HuyenModel()
        {
        }
        public HuyenModel(string json)
        {
            JObject jObject = JObject.Parse(json);
            tinh = (string)jObject["Ma_Tinh"];
            maHuyen = (string)jObject["Ma_Huyen"];
            tenHuyen = (string)jObject["Ten_Huyen"];
            ghiChu = (string)jObject["GhiChu"];
        }

        public List<HuyenModel> GetHuyenModel()
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create("" + Util.getUrl() + "Home/GetHuyenModel");

            return GetHuyen(request);
        }

        public List<HuyenModel> GetHuyenByTinh(string maTinh)
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create("" + Util.getUrl() + "Home/GetHuyenByTinh?maTinh=" + maTinh);

            return GetHuyen(request);
        }

        private static List<HuyenModel> GetHuyen(System.Net.WebRequest request)
        {
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json; charset=utf-8";
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            //get response-stream, and use a streamReader to read the content
            List<HuyenModel> listHuyenModel = new List<HuyenModel>();
            using (Stream s = request.GetResponse().GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    JavaScriptSerializer jsonserializer = new JavaScriptSerializer();
                    listHuyenModel = jsonserializer.Deserialize<List<HuyenModel>>(sr.ReadToEnd());
                    return listHuyenModel;
                }
            }
            return new List<HuyenModel>();
        }

        private string tinh;

        public string Tinh
        {
            get { return tinh; }
            set { tinh = value; }
        }
        private string maHuyen;

        public string MaHuyen
        {
            get { return maHuyen; }
            set { maHuyen = value; }
        }
        private string tenHuyen;

        public string TenHuyen
        {
            get { return tenHuyen; }
            set { tenHuyen = value; }
        }
        private string ghiChu;

        public string GhiChu
        {
            get { return ghiChu; }
            set { ghiChu = value; }
        }

    }
}
